package com.kingint.web.portal.serve.controller;


import com.kingint.common.unitedresult.K;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.ser.entity.SerServiceOrder;
import com.kingint.web.portal.serve.service.ServiceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/serviceOrder")
@Api(value = "ServiceOrderController", description = "前端服务工单控制层")
@CrossOrigin
public class ServiceOrderController {
    @Resource
    private ServiceOrderService serviceOrderService;


    @ApiOperation(value = "添加服务工单表")
    @PostMapping("/create")
    public Object create(@Validated(Insert.class) @RequestBody SerServiceOrder serServiceOrder) {
        int i = serviceOrderService.insertServiceOrderInfo(serServiceOrder);
        if (i > 0) {
            return K.ok().data(serServiceOrder.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "获取待处理服务工单信息")
    @GetMapping("/stayHandleServiceOrder")
    public Object stayHandleServiceOrder() {
        List<SerServiceOrder> list = serviceOrderService.stayHandleServiceOrder();
        return K.ok().data(list);
    }

    @ApiOperation(value = "对应服务单待接单，接单")
    @GetMapping("/receiverServiceOrder")
    public Object receiverServiceOrder(@RequestParam("id") Integer id) {
        Integer i=serviceOrderService.receiverServiceOrder(id);
        if(i>0){
            return K.ok().data(i);
        }
        return K.error();
    }

    //查询对应服务工单的详情
    @ApiOperation(value = "获取对应服务工单的详情")
    @GetMapping("/getServiceOrderDetails")
    public Object getServiceOrderDetails(@RequestParam(value = "id") Integer id) {
        SerServiceOrder info= serviceOrderService.getServiceOrderDetails(id);
        return K.ok().data(info);
    }

    @ApiOperation(value = "获取待确认服务工单（已经指定处理人）")
    @GetMapping("/stayConfirmServiceOrder")
    public Object stayConfirmServiceOrder() {
        List<SerServiceOrder> list = serviceOrderService.stayConfirmServiceOrder();
        return K.ok().data(list);
    }

    @ApiOperation(value = "处理人确认处理")
    @GetMapping("/confirmServiceOrder")
    public Object confirmServiceOrder(@RequestParam(value = "id") Integer id) {
        int i=serviceOrderService.confirmServiceOrder(id);
        if(i>0){
            return K.ok().data(i);
        }
        return K.error();
    }

    @ApiOperation(value = "完成服务单处理")
    @GetMapping("/finishServiceOrder")
    public Object finishServiceOrder(@RequestParam(value = "id") Integer id) {
        int i=serviceOrderService.finishServiceOrder(id);
        if(i>0){
            return K.ok().data(i);
        }else if(i==-1){
            return K.error().data("当前工单挂起，不能完成该工单");
        }
        return K.error();
    }

    @ApiOperation(value = "挂起此单")
    @GetMapping("/hangUpOrder")
    public Object hangUpOrder(@RequestParam(value = "id") Integer id) {
        int i=serviceOrderService.hangUpOrder(id);
        if(i>0){
            return K.ok().data(i);
        }
        return K.error().data("该工单已经挂起");
    }
    //更换处理人
    @ApiOperation(value = "更换处理人")
    @GetMapping("/changeHandleUser")
    public Object changeHandleUser(@RequestParam(value = "id") Integer id,@RequestParam("memberId") Long memberId) {
        int i=serviceOrderService.changeHandleUser(id,memberId);
        if(i>0){
            return K.ok().data(i);
        }
        return K.error();
    }
}
